package com.zzz.system.manager;

import com.zzz.common.core.constants.CacheConstants;
import com.zzz.common.redis.service.RedisService;
import com.zzz.system.domain.exam.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: oj-project
 * @BelongsPackage: com.zzz.friend.manager
 * @Author: zhai
 * @CreateTime: 2025-11-01  15:39
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class ExamCacheManager {

    @Autowired
    private RedisService redisService;

    /**
     * @description: 添加缓存(当发布竞赛成功的时候需要：保存竞赛的基本信息和保存未完赛的列表中)
     * @author: zhai
     * @date: 2025/11/1 15:42
     * @param: [exam]
     * @return: void
     **/
    public void addCache(Exam exam) {
        redisService.leftPushForList(getExamListKey(exam.getExamId()), exam);
        redisService.setCacheObject(getExamDetail(exam.getExamId()), exam);
    }

    /**
     * @description: 删除竞赛缓存(当取消发布的时候需要删除：列表中的缓存和存储的竞赛的基本信息的缓存)
     * @author: zhai
     * @date: 2025/11/1 15:43
     * @param: [exam]
     * @return: void
     **/
    public void deleteCache(Exam exam) {
        redisService.removeForList(getExamListKey(exam.getExamId()), exam);
        redisService.deleteObject(getExamDetail(exam.getExamId()));
    }

    public String getExamListKey(Long examId) {
        return CacheConstants.EXAM_UNFINISHED_LIST + examId;
    }

    public String getExamDetail(Long examId) {
        return CacheConstants.EXAM_DETAIL + examId;
    }
}
